import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/login/area_widget/area_code_widget.dart';
import 'package:full_display/routers/route_name.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/navigation/navigation_util.dart';
import 'package:full_display/unit/screen/screen_utility.dart';

class PhoneRegister extends StatefulWidget {
  final String codeStr;
  final Function(Map<String, String>) callBack;
  final Function registerCodeCallBack;

  const PhoneRegister({
    Key? key,
    required this.codeStr,
    required this.callBack,
    required this.registerCodeCallBack,
  }) : super(key: key);

  @override
  State<PhoneRegister> createState() => _PhoneRegisterState();
}

class _PhoneRegisterState extends State<PhoneRegister> {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _passwordConfigController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  final map = {'phoneNum': '', 'password': '', 'passwordConfig': '', 'verificationCode': '', 'areaCode': '+86'};

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              GestureDetector(
                onTap: () {
                  NavigationUtil.getInstance()
                      .pushPage(context, RouteNames.areaCodeWidget, widget: const AreaCodeWidget(), func: (value) {
                    setState(() {
                      map['areaCode'] = value['key'];
                    });
                  });
                },
                child: Row(
                  children: [
                    Text(
                      map['areaCode'] ?? '+86',
                      style: StandardTextStyle.inputHintStyle,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 4.0.W),
                      width: 14.0.W,
                      height: 8.0.H,
                      child: Image.asset(AssetUtils.getAssetImage('icon_down_arrow')),
                    )
                  ],
                ),
              ),
              Expanded(
                  child: Container(
                margin: EdgeInsets.only(left: 10.0.W),
                child: TextField(
                  maxLines: 1,
                  maxLength: 11,
                  keyboardType: TextInputType.number,
                  inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                  onChanged: (text) {
                    _handleInput('phoneNum', text);
                  },
                  controller: _phoneController,
                  style: StandardTextStyle.inputHintStyle,
                  decoration: InputDecoration(
                      counterText: '',
                      hintText: '请输入手机号',
                      hintStyle: StandardTextStyle.inputHintStyle,
                      border: InputBorder.none),
                ),
              ))
            ],
          ),
          Divider(
            height: 0.5.H,
            color: ColorPlate.white.withOpacity(0.15),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                  child: Container(
                height: 67.0.H,
                margin: EdgeInsets.only(left: 71.0.W),
                child: TextField(
                  onChanged: (text) {
                    _handleInput('password', text);
                  },
                  controller: _passwordController,
                  style: StandardTextStyle.inputHintStyle,
                  inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\W]'))],
                  decoration: InputDecoration(
                      counterText: '',
                      hintText: '请输入密码',
                      hintStyle: StandardTextStyle.inputHintStyle,
                      border: InputBorder.none),
                ),
              )),
            ],
          ),
          Divider(
            height: 0.5.H,
            color: ColorPlate.white.withOpacity(0.15),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                  child: Container(
                height: 67.0.H,
                margin: EdgeInsets.only(left: 71.0.W),
                child: TextField(
                  onChanged: (text) {
                    _handleInput('passwordConfig', text);
                  },
                  controller: _passwordConfigController,
                  style: StandardTextStyle.inputHintStyle,
                  inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\W]'))],
                  decoration: InputDecoration(
                      counterText: '',
                      hintText: '请再次输入密码',
                      hintStyle: StandardTextStyle.inputHintStyle,
                      border: InputBorder.none),
                ),
              )),
            ],
          ),
          Divider(
            height: 0.5.H,
            color: ColorPlate.white.withOpacity(0.15),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                  child: Container(
                height: 67.0.H,
                margin: EdgeInsets.only(left: 71.0.W),
                child: TextField(
                  maxLines: 1,
                  maxLength: 6,
                  keyboardType: TextInputType.number,
                  inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                  controller: _codeController,
                  onChanged: (text) {
                    _handleInput('verificationCode', text);
                  },
                  style: StandardTextStyle.inputHintStyle,
                  decoration: InputDecoration(
                      counterText: '',
                      hintText: '请输入验证码',
                      hintStyle: StandardTextStyle.inputHintStyle,
                      border: InputBorder.none),
                ),
              )),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                    widget.registerCodeCallBack.call();
                },
                child: Text(
                  widget.codeStr,
                  style: StandardTextStyle.textBlue16Style,
                ),
              )
            ],
          ),
          Divider(
            height: 0.5.H,
            color: ColorPlate.white.withOpacity(0.15),
          ),
        ],
      ),
    );
  }


  _handleInput(String type, String text) {
    switch (type) {
      case 'phoneNum':
        map['phoneNum'] = text;
        break;
      case 'password':
        map['password'] = text;
        break;
      case 'passwordConfig':
        map['passwordConfig'] = text;
        break;
      case 'verificationCode':
        map['verificationCode'] = text;
        break;
    }
    widget.callBack.call(map);
  }

  @override
  void dispose() {
    super.dispose();
  }
}
